﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data.Odbc;
using System.Data.OleDb;

namespace WFDataBaseProject.Controls
{
    /// <summary>
    /// Interaction logic for UCConnectionStringEditor.xaml
    /// </summary>
    public partial class UCConnectionStringEditor : UserControl
    {
        public UCConnectionStringEditor()
        {
            InitializeComponent();
        }
         
        public string ConnectionString
        {
            get { return (string)GetValue(ConnectionStringProperty); }
            set { SetValue(ConnectionStringProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ConnectionString.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ConnectionStringProperty =
            DependencyProperty.Register("ConnectionString", typeof(string), typeof(UCConnectionStringEditor), new UIPropertyMetadata(string.Empty));



        private void btnTestConnection_Click(object sender, RoutedEventArgs e)
        {  
            switch (cmbConnectionType.SelectedIndex)
            {
                case 0:
                    {
                        using (SqlConnection sqlconnection = new SqlConnection(txtConnectionString.Text))
                        {
                            try
                            {
                                sqlconnection.Open();
                                Communicator.ConnectionString = sqlconnection.ConnectionString;
                                Communicator.ConnectionType = Helpers.TypeOfConnection.SqlServerConnection;
                                MessageBox.Show("Connection succeeds");
                            }
                            catch (SqlException caught)
                            {
                                MessageBox.Show(string.Format("Connection failed: {0}", caught.Message));
                            }
                            finally { sqlconnection.Close(); } 
                        }          
                   }
                   break;
                case 1:
                    {
                        using (OdbcConnection odbcconnection = new OdbcConnection(txtConnectionString.Text))
                        {
                            try
                            {
                                odbcconnection.Open();
                                Communicator.ConnectionString = odbcconnection.ConnectionString;
                                Communicator.ConnectionType = Helpers.TypeOfConnection.ODBCConnection;
                                MessageBox.Show("Connection succeeds");
                            }
                            catch (OdbcException caught)
                            {
                                MessageBox.Show(string.Format("Connection failed: {0}", caught.Message));
                            }
                            finally { odbcconnection.Close(); }
                        }
                    }
                    break;
                case 2:
                    {
                        using (OleDbConnection oledbconnection = new OleDbConnection(txtConnectionString.Text))
                        {
                            try
                            {
                                oledbconnection.Open();
                                Communicator.ConnectionString = oledbconnection.ConnectionString;
                                Communicator.ConnectionType = Helpers.TypeOfConnection.OledbConnection;
                                MessageBox.Show("Connection succeeds");
                            }
                            catch (OdbcException caught)
                            {
                                MessageBox.Show(string.Format("Connection failed: {0}", caught.Message));
                            }
                            finally { oledbconnection.Close(); }
                        }
                    }
                    break;
                case 3:
                    throw new NotImplementedException("Not implemented");
                default:
                    throw new NotImplementedException("Not implemented");
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            ConnectionString = txtConnectionString.Text;
            (this.Parent as Window).Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            ConnectionString = string.Empty;
            (this.Parent as Window).Close();
        }

    }
}
